import React, { Component } from 'react'
import { FormattedMessage } from 'react-intl'
import { hashHistory } from 'react-router'
import { Toast, Button } from 'antd-mobile'
import { LimitOrStopPrice  } from '../trade'
import { helper, myFetch, config } from '../utils'

export default class PositionOrderDirect extends Component {

  onEdit = (data) => {
    console.log(data)
    const options = {
      method: 'POST',
      body: JSON.stringify({
        ORDR: data[0].orderNo,
        OFLG: 1,
        PRIC: this.state.price
      })
    }
    myFetch(`${config.rootApi}/mEdit`,options, true )
    .then((rs) => {
      if (rs && rs.statusCode == 0) {
        Toast.success('修改挂单成功', 1.2, () => {
          hashHistory.push('/position')
        })
      }
    })
  }
  constructor(props) {
    super(props)
    this.state = {
      data: props.data
    }
  }
  componentWillReceiveProps(nextProps) {
    console.log(nextProps.data)
    if (nextProps.data != this.props.data) {
      this.setState({
        data: nextProps.data,
      })
    }
  }
  onChange = (price) => {
    this.setState({
      price
    })
  }
  render() {
    const data = this.state.data
    return (
      <div className="rt-position-direct">
        <div className="rt-order-title">
          <FormattedMessage id="mainsetting"/>
        </div>
        <LimitOrStopPrice
          defaultCon={data[0] ? data[0].conditionCls : 3}
          price={data[0] ? data[0].orderPrice : 0}
          ccy={data[0] ? data[0].ccyPair : 'EUR/USD'}
          onChange={this.onChange}
        />
        <Button className="btn rt-sure " type="primary"
          onClick={
            () => {
              this.onEdit(data)
            }
          }
        >
          <FormattedMessage
            id='confirm'
           /> 
        </Button>
      </div>
    )
  }
}
